{extend name="admin/base" /}
{block name="main"}
<!-- page -->
<form class="form-horizontal widthFixedForm" role="form">
    <input type="hidden" id="uid" value="{$ua_info.uid}" />
    <div class="form-group">
        <label for="mobile" class="col-md-2 control-label"><span class="red">*</span>手机号</label>
        <div class="col-md-8">
            <input type="number" class="form-control" id="mobile" name="mobile"  value="{$ua_info['mobile']}" title="请输入手机号" required autocomplete="off">
        </div>
    </div>
    <!--使用人-->
    <div class="form-group">
        <label for="user" class="col-md-2 control-label">使用人</label>
        <div class="col-md-8">
            <input type="text" class="form-control" id="user" name="user" value="{$ua_info['user']}" autocomplete="off">
        </div>
    </div>
    <!--角色下拉框-->
    <div class="form-group">
        <label for="group_id" class="col-md-2 control-label">角色</label>
        <div class="col-md-8">
            <select class="form-control" name="group_id" id="group_id">
                <option value="0">请选择角色</option>
                {foreach name="auth_group" item="v1"}
                <option value="{$v1['group_id']}" {if condition="$v1['group_id'] eq $ua_info['group_id_array']"} selected {/if} >&nbsp;&nbsp;{$v1['group_name']}</option>
                {/foreach}
            </select>
        </div>
    </div>
    <!--添加返回-->
    <div class="form-group add_back">
        <div class="col-md-2"></div>
        <div class="col-md-8">
            <button type="button" class="btn add">提交</button>
            <a href="{:__URL('ADMIN_MAIN/auth/userlist')}" class="btn back">返回</a>
        </div>
    </div>
</form>
<!-- page end -->
{/block}
{block name="script"}
<script>
 require(['utilAdmin'], function (utilAdmin) {
    var flag = false;//防止重复提交
//添加用户
    function editUserAjax() {
        var uid = $("#uid").val();
        var group_id = $("#group_id").val();
        var user = $("#user").val();
        var mobile = $("#mobile").val();
        if(mobile==''){
            utilAdmin.message('手机号不能为空！','info', function () {
                $('#mobile').focus();
            });
            return false;
        }
        
        if (group_id === '') {
            utilAdmin.message('请选择角色！','info', function () {
                $('#group_id').focus();
            });
            return false;
        }
        if (user === '') {
            utilAdmin.message('请输入使用人！', 'info', function () {
                $('#user').focus();
            });
            return false;
        }
        if (!flag) {
            flag = true;
            $.ajax({
                type: "post",
                url: "{:__URL('ADMIN_MAIN/auth/edituser')}",
                data: {
                    'group_id': group_id,
                    'uid' : uid,
                    'user': user,
                    'mobile':mobile
                },
                success: function (data) {
                    if (data['code'] > 0) {
                        utilAdmin.message(data["message"],'success', function () {
                            flag = false;
                            location.href = "{:__URL('ADMIN_MAIN/auth/userlist')}";
                        });
                    } else {
                        if(data['code'] ==-2){
                            flag = false;
                            utilAdmin.message('该手机号已存在','danger');
                        }else{
                            flag = false;
                            utilAdmin.message(data['message'],'danger');
                        }
                    }
                }
            });
        }
    }

    $("body").on("click",'.add',function(){
        editUserAjax();
    })

 })
</script>
{/block}